// components/choose-asset/choose-asset.js
import { getAssetListPage } from '@/api/assetsBorrow.js'
Component({

  /**
   * 组件的属性列表
   */
  properties: {
    showAssetPopup: {
      type: Boolean,
      value: false
    },
    status:{
      type:Number,
      value:0
    },
    assignedTo:{
      type:Number,
      value:null
    },
    assetsIds:{
      type:Array,
      value:[]
    }
  },

  observers: {
    'showAssetPopup': function (newVal) {
      this.setData({
        assetShow: newVal
      })
    }
  },
  /**
   * 组件的初始数据
   */
  data: {
    assetShow:false,
    assetName:"",
    assets: [
      { 
        id: 1, 
        name: '笔记本电脑', 
        categoryName:"分类",
        assetCode:"122资产编号",
        model:"型号",
        deptName:"所在部门",
        serialNumber: "序列号",
        locationName:"位置名称",
        pic: 'https://img.yzcdn.cn/vant/cat.jpeg', 
        status: "状态"
       },
      { 
        id: 2, 
        name: '笔记本电脑', 
        categoryName:"分类",
        assetCode:"122资产编号",
        model:"型号",
        deptName:"所在部门",
        serialNumber: "序列号",
        locationName:"位置名称",
        pic: 'https://img.yzcdn.cn/vant/cat.jpeg', 
        status: "状态"
       },
        { 
        id: 3, 
        name: '笔记本电脑', 
        categoryName:"分类",
        assetCode:"122资产编号",
        model:"型号",
        deptName:"所在部门",
        serialNumber: "序列号",
        locationName:"位置名称",
        pic: 'https://img.yzcdn.cn/vant/cat.jpeg', 
        status: "状态"
       },
    ],
    selectedAssets: [],
    searchKeyword: "",
    queryOption: {
      pageNum: 1,
      pageSize: 10,
      name:"",
      status:"",
      assignedTo:0,
    },
    //总数
    total: 0,
    //分了几页
    pages: 0,
    refresherTriggered: false, // 新增字段用于控制下拉刷新状态
  },

  /**
   * 组件的方法列表
   */
  methods: {
    // 关闭弹窗
    onPopupClose(){
      this.setData({
        assetShow: false,
        searchKeyword: ""
      })
    },
    // 点击弹框确认
    onPopupConfirm(){
      this.setData({
        assetShow: false,
        searchKeyword: ""
      })
    },
    // 输入时更新 searchKeyword
    onSearchInput(e) {
      this.setData({
        searchKeyword: e.detail
      });
    },
    // 点击资产搜索后面的搜索按钮
    onSearchClick(){
      this.setData({
        'queryOption.name': this.data.searchKeyword,
        'queryOption.pageNum': 1 // 重置分页
      });

      this.getAssetList(); // 请求新数据
      console.log(123,this.data.queryOption)
    },
    // 选择资产
    onAssetsChange(event) {
      console.log('父亲event:',event.detail.selectedAssets);
      this.setData({
        selectedAssets: event.detail.selectedAssets
      });
      this.triggerEvent('assetChange', this.data.selectedAssets);
    },
    getAssetList(){
      return new Promise((resolve, reject) => {
        getAssetListPage(this.data.queryOption).then(res=>{
          console.log(res.data,"资产列表");
          // 判断是否是第一页，如果是则直接赋值，否则追加数据
          const newAssets = this.data.queryOption.pageNum === 1 ? res.data.list : [...this.data.assets, ...res.data.list];
          this.setData({
            assets: newAssets,
            total: res.data.total,
            pages: res.data.pages
          });
          resolve();
        }).catch(err => {
          reject(err);
        });
      });
    },
    // 下拉刷新
    refresherHandle(){
      this.setData({
        queryOption: {
          pageNum: 1,
          pageSize: 10,
          name: "",
          status: this.data.status,
          assignedTo: this.data.assignedTo
        },
        refresherTriggered: true // 开始刷新
      });
      this.getAssetList().then(() => {
        this.setData({
          refresherTriggered: false // 刷新完成
        });
      }).catch(() => {
        this.setData({
          refresherTriggered: false // 刷新失败也停止刷新
        });
      });
    },
    // 获取更多数据
    getMoreData(){
      console.log(1122)
      if(this.data.queryOption.pageNum >= this.data.pages){
        wx.showToast({
          title: '没有更多数据了',
          icon: 'none'
        });
        return;
      }
      this.data.queryOption.pageNum++;
      this.getAssetList();
    },
    // 搜索资产
    searchAsset(){
      this.data.queryOption.pageNum = 1;
      this.getAssetList();
    }
  },
  lifetimes:{
    attached() {
      this.setData({
        searchKeyword: this.data.queryOption.name,
        ['queryOption.status']:this.properties.status,
        ['queryOption.assignedTo']:this.properties.assignedTo
      });
      this.getAssetList();
    }
  },
})